#!/bin/bash
# Requirement: tesseract directory name should be "tesseract-ocr"

source_dir=${PWD##*/}
tess_ver=3.02
src_min_ver=.02
ver=$source_dir-$tess_ver
excld="exclude.txt"

cd ..
echo Output for $source_dir is in `pwd` ...

# create language files packages
if [ -f $excld ] 
then
	echo Removing $excld...
	rm -f $excld
fi

for l in $source_dir/tessdata/*.traineddata
do
	filename=`basename "$l"`
	lang=${filename%.*}
	echo  Creating language package for `basename "$l"`...
	chmod 644 $source_dir/tessdata/*$lang*
	tar --group root --owner root -chozf $ver.`basename "$l" .traineddata`.tar.gz $source_dir/tessdata/*$lang*
	echo $source_dir/tessdata/*$lang* | tr " " "\n" >>$excld
done

# Windows build relevant files
name=$ver-win_vs2008
# Let's be nice and use common windows packager ;-)
zip -9 -r --exclude=*.svn* --exclude=*strtok_r* $name.zip $source_dir/vs2008/

# create linux source package
echo Creating linux source package $ver$src_min_ver.tar.gz ...
tar -chof $ver$src_min_ver.tar --exclude=.svn --exclude=debian --exclude=autom4te.cache --exclude=doc/html --exclude=po/ --exclude=m4/ --exclude=makemoredists --exclude=vs2008 -exclude=INSTALL.SVN -X $excld $source_dir/*
#workaround to include strtok_r from vs2008 for mingw
tar -rof $ver$src_min_ver.tar $source_dir/vs2008/port/strtok_r.*
gzip $ver$src_min_ver.tar

# create doc and package doc
cd $source_dir
doxygen doc/Doxyfile
cd ..
tar --group root --owner root -chozf $ver-doc-html.tar.gz $source_dir/doc/html/*

rm -f $excld
cd $source_dir
